// src/services/workoutService.ts
import { WorkoutPlan } from '../models/WorkoutPlan';
import { WorkoutRecord } from '../models/WorkoutRecord';

const API_URL = 'https://your-api-url.com/api';

export const getWorkoutPlans = async (userId: string): Promise<WorkoutPlan[]> => {
    const response = await fetch(`${API_URL}/workout-plans?userId=${userId}`);
    if (!response.ok) throw new Error('获取计划失败');
    return response.json();
};

export const createWorkoutPlan = async (plan: Omit<WorkoutPlan, 'id' | 'createdAt'>): Promise<WorkoutPlan> => {
    const response = await fetch(`${API_URL}/workout-plans`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(plan)
    });

    if (!response.ok) throw new Error('创建计划失败');
    return response.json();
};

export const startWorkout = async (planId: string, userId: number): Promise<WorkoutRecord> => {
    const response = await fetch(`${API_URL}/workout-records/start`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ planId, userId })
    });

    if (!response.ok) throw new Error('开始锻炼失败');
    return response.json();
};

export const completeWorkout = async (record: WorkoutRecord): Promise<WorkoutRecord> => {
    const response = await fetch(`${API_URL}/workout-records/${record.id}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(record)
    });

    if (!response.ok) throw new Error('完成锻炼失败');
    return response.json();
};